package com.heatup.apt.management.controller;

import com.heatup.apt.common.dao.ChannerDetailedMapper;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.management.service.channer.ChannerDetailedService;
import com.heatup.apt.management.service.channer.ChannerQrcodeService;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.channer.ChannerQrcode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * 渠道二维码控制器
 * @author jixiangyun
 * 2016年11月23日 上午11:19:41
 */

@Controller
@RequestMapping("system/channel")
public class ChannelController {
	private Logger logger = LoggerFactory.getLogger(ChannelController.class); 
	
	@Autowired
	private ChannerQrcodeService channerQrcodeService;
	
	@Autowired
	private ChannerDetailedMapper channerDetailedMapper;
	
	@Autowired
	private ChannerDetailedService channerDetailedService;
	
	/**
	 * 打开渠道二维码list界面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/to_channer_list")
	public ModelAndView to_channer_list(HttpServletRequest request , HttpServletResponse response){
		ModelAndView model = new ModelAndView();
		String mobile = RequestTools.RequestString(request, "mobile",null);
		String channelName = RequestTools.RequestString(request, "channelName",null);
		int status = RequestTools.RequestInt(request, "status", -2);
		int pageNum = RequestTools.RequestInt(request, "pageNum", 1);
		Map<String,Object> map = new HashMap<>();
		map.put("mobile", mobile);
		map.put("channelName", channelName);
		map.put("status", status);
		map.put("pageNum", pageNum);
		Map<String, Object> objectMap = channerQrcodeService.queryChannerQrcodeList(map);
		model.addAllObjects(objectMap);
		model.setViewName("operationsmanagement/channel_user_list");
		return model;
	}
	
	/**
	 * 打开添加界面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/to_channer_and")
	public ModelAndView to_channer_and(HttpServletRequest request , HttpServletResponse response){
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		ModelAndView model = new ModelAndView();
		model.setViewName("operationsmanagement/channel_code_add");
		return model;
	}
	
	/**
	 * 添加渠道二维码
	 * @param channerQrcode
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/channer_save")
	public String channer_and(ChannerQrcode  channerQrcode,HttpServletRequest request) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		String callback = RequestTools.RequestString(request, "callback", "");
		try {
			apiResultModel = channerQrcodeService.saveChannerQrcode(channerQrcode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			apiResultModel.setErrorCode(-1);
			e.printStackTrace();
		}
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	
	/**
	 * 编辑界面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/channer_edit")
	public ModelAndView channer_edit(HttpServletRequest request , HttpServletResponse response) {
		int id = RequestTools.RequestInt(request, "id", 0);
		ModelAndView model = new ModelAndView();
		Map<String, Object> objectMap = new HashMap<>();
		objectMap.put("channer",channerQrcodeService.queryChannerQrcode(id));
		model.addAllObjects(objectMap);
		model.setViewName("operationsmanagement/channel_code_edit");
		return model;
	}
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/channer_del")
	public String channer_del(HttpServletRequest request , HttpServletResponse response) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		String callback = RequestTools.RequestString(request, "callback", "");
		int id = RequestTools.RequestInt(request, "id", 0);
		ChannerQrcode channerQrcode = channerQrcodeService.queryChannerQrcode(id);
		channerQrcode.setStatus(-1);
		int bool = channerQrcodeService.updateQRCode(channerQrcode);
		apiResultModel.setData(bool);
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	@RequestMapping("/to_channer_detailed")
	public ModelAndView to_channer_detailed(HttpServletRequest request , HttpServletResponse response){
		ModelAndView model = new ModelAndView();
		String channelId = RequestTools.RequestString(request, "channelId",null);
		String userId = RequestTools.RequestString(request, "userId",null);
		int pageNum = RequestTools.RequestInt(request, "pageNum", 1);
		Map<String,Object> map = new HashMap<>();
		map.put("channelId", channelId);
		map.put("userId", userId);
		map.put("pageNum", pageNum);
		Map<String, Object> objectMap = channerDetailedService.queryChannerDetailedList(map);
		model.addAllObjects(objectMap);
		model.setViewName("operationsmanagement/channel_detailed_list");
		return model;
	}
	
	
	@RequestMapping("/url")
	public ModelAndView urlToLoginUrl(HttpServletRequest request , HttpServletResponse response){
		ModelAndView model = new ModelAndView();
		
		model.setViewName("operationsmanagement/url_to_login");
		return model;
	}
	
}
